package com.nai365.nart.order.service.impl;

import cn.hutool.core.lang.Console;
import com.alibaba.fastjson.JSONObject;
import com.nai365.nart.blockchain.api.feign.RemoteDDCService;
import com.nai365.nart.common.core.constant.SecurityConstants;
import com.nai365.nart.common.core.vo.WalletChangeVo;
import com.nai365.nart.member.feign.WalletRemoteService;
import com.nai365.nart.order.entity.Order;
import com.nai365.nart.order.service.OrderService;
import com.nai365.nart.order.service.PayCallBackService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@RequiredArgsConstructor
public class PayCallBackServiceImpl implements PayCallBackService {
	@Autowired
	private OrderService orderService;

	private final WalletRemoteService walletRemoteService;


	private final RemoteDDCService remoteDDCService;


	/**
	 * 提现
	 * @param callback
	 */
	@Override
	public void payCallback(String callback) {
		/**
		 * 回调5次  5提现
		 */
		JSONObject jsonObject = JSONObject.parseObject(callback);
		String status = jsonObject.getString("status");
		String requestId = jsonObject.getString("requestId");
		String orderAmount = jsonObject.getString("orderAmount");

		Order order = orderService.getOrderByOrderSn(requestId);
		Console.log("################################payCallback");
		Console.log(jsonObject);
		/**
		 * 提现回调回来的金额是分
		 */
		if(order.getOrderType()==5){
			//1.钱包充值
//			Wallet wallet = new Wallet();
//			wallet.setUserId(order.getMemberId());
			BigDecimal divide = new BigDecimal(orderAmount).divide(BigDecimal.valueOf(100L)).negate();
//			wallet.setNewRmbBalance(divide);
//			walletRemoteService.updateById(wallet, SecurityConstants.FROM_IN);
			//2.流水记录
			WalletChangeVo walletChangeVo = new WalletChangeVo();
			walletChangeVo.setFromUserId(order.getMemberId());
			walletChangeVo.setToUserId(order.getMemberId());
			walletChangeVo.setOrderSn(requestId);
			walletChangeVo.setRmbValue(divide);
			walletChangeVo.setType(order.getOrderType());
			walletChangeVo.setSkuName("提现");

			walletRemoteService.updateByIdInner(walletChangeVo,SecurityConstants.FROM_IN);
		}
		//3.订单状态修改
		//switch
		updateOrderStatus(status,requestId);



	}

	/**
	 * 支付
	 * @param callback
	 */
	@Override
	public void drawCallback(String callback) {
		JSONObject jsonObject = JSONObject.parseObject(callback);
		/**
		 * 回调5次 订单类型
		 * 订单类型（1优先购 2抢购 3抽签 4充值 5提现6二级市场）
		 */
		String status = jsonObject.getString("status");
		String requestId = jsonObject.getString("requestId");
		String orderAmount = jsonObject.getString("orderAmount");

		Order order = orderService.getOrderByOrderSn(requestId);


		WalletChangeVo walletChangeVo = new WalletChangeVo();
		walletChangeVo.setFromUserId(order.getMemberId());
		walletChangeVo.setToUserId(order.getMemberId());
		walletChangeVo.setOrderSn(requestId);


		//充值 钱包有流水记录，其他正常下单不涉及流水
		if(order.getOrderType()==4){
			BigDecimal divide = new BigDecimal(orderAmount).divide(BigDecimal.valueOf(100L));
			walletChangeVo.setRmbValue(divide);
			walletChangeVo.setSkuName("充值");
			walletChangeVo.setType(order.getOrderType());
			walletRemoteService.updateByIdInner(walletChangeVo,SecurityConstants.FROM_IN);
		}
//		else {
//			BigDecimal divide = new BigDecimal(orderAmount).divide(BigDecimal.valueOf(100L)).negate();
//			walletChangeVo.setRmbValue(divide);
//			walletChangeVo.setSkuName("支付");
//		}



		////3.订单状态修改
		updateOrderStatus(status,requestId);
	}


	private void updateOrderStatus(String status,String requestId){
		switch (status){
			case "SUCCESS":
				orderService.updateOrderStatusByOserId(requestId,3);
				break;
			case "FAILED":
				orderService.updateOrderStatusByOserId(requestId,4);
				break;

			default:
				orderService.updateOrderStatusByOserId(requestId,5);
		}
	}


}
